package org.thirdHero.admin.web;

import org.thirdHero.admin.annotation.LoginAdmin;
import org.thirdHero.core.token.UserTokenManager;
import org.thirdHero.core.util.ResponseUtil;
import org.thirdHero.core.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.thirdHero.db.domain.*;
import org.thirdHero.db.service.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
public class AdminController {
    @Resource
    private LitemallAdminService adminService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRelationService sysRelationService;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private UserTokenManager userTokenManager;
    @GetMapping("/info")
    public Object info(@LoginAdmin SysUser sysUser){
        SysUser admin = sysUserService.findById(sysUser.getId());
        if(admin == null){
            return ResponseUtil.badArgumentValue();
        }

        Map<String, Object> data = new HashMap<>();
        data.put("name", admin.getName());
        data.put("avatar", admin.getAvatar());

        List<String> roles = new ArrayList<>();
        SysRole sysRole = sysRoleService.queryRoleById(Integer.parseInt(admin.getRoleId()));
        roles.add(sysRole.getName());

        List<SysRelation> sysRelations = sysRelationService.queryRelationByRoleId(Integer.parseInt(admin.getRoleId()));
        List<String> menus = new ArrayList<>();
        for(SysRelation sysRelation : sysRelations){
            if(sysRelation==null){
                continue;
            }
            SysMenu liteMallMenu = sysMenuService.findLiteMallMenuById(sysRelation.getMenuid());
            if(liteMallMenu!=null){
                menus.add(liteMallMenu.getCode());
            }
        }

        data.put("roles", roles);
        data.put("menus",menus);
        data.put("introduction", "admin introduction");
        return ResponseUtil.ok(data);
    }

    @GetMapping("/list")
    public Object list(@LoginAdmin SysUser sysUser ,
                       String username,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                       String sort, String order){

        List<LitemallAdmin> adminList = adminService.querySelective(username, page, limit, sort, order);
        int total = adminService.countSelective(username, page, limit, sort, order);
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", adminList);

        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@LoginAdmin SysUser sysUser , @RequestBody LitemallAdmin admin){

        String rawPassword = admin.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(rawPassword);
        admin.setPassword(encodedPassword);

        adminService.add(admin);
        return ResponseUtil.ok(admin);
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin SysUser sysUser , Integer id){

        if(id == null){
            return ResponseUtil.badArgument();
        }

        LitemallAdmin admin = adminService.findById(id);
        return ResponseUtil.ok(admin);
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin SysUser sysUser , @RequestBody LitemallAdmin admin){

        Integer anotherAdminId = admin.getId();
        if(anotherAdminId.intValue() == 1){
            return ResponseUtil.fail(403, "超级管理员不能修改");
        }

        adminService.updateById(admin);
        return ResponseUtil.ok(admin);
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin SysUser sysUser , @RequestBody LitemallAdmin admin){

        Integer anotherAdminId = admin.getId();
        if(anotherAdminId.intValue() == 1){
            return ResponseUtil.fail(403, "超级管理员不能删除");
        }
        adminService.deleteById(anotherAdminId);
        return ResponseUtil.ok();
    }
}
